package utils.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * @version:1.0
 * @param <T>
 * @Todo:通用适配器
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    
    protected List<T> list;
    
    protected Context context;
    
    protected int resId;
    
    public CommonAdapter(List<T> list, Context context, int resId) {
        this.list = list;
        this.context = context;
        this.resId = resId;
    }
    
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    
    @Override
    public T getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return list.get(position).hashCode();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = getViewHolder(convertView, parent);
        convert(holder, position, getItem(position));
        return holder.getContentView();
    }
    
    public abstract void convert(ViewHolder vh, int position, T t);
    
    public ViewHolder getViewHolder(View convertView, ViewGroup parent) {
        return ViewHolder.get(context, parent, convertView, resId);
    }
    public List<T> getList() {
        return list;
    }
}
